package com.tm.szb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.szb.dto.EmployeeDTO;
import com.tm.szb.entity.Employee;
import com.tm.szb.enums.StatusEnums;
import com.tm.szb.mapper.EmployeeMapper;
import com.tm.szb.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public Employee getByUserName(String username) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void addEmployee(EmployeeDTO employeeDTO, Long employeeId) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        //设置初始化密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置默认为启用状态
        employee.setStatus(StatusEnums.ENABLE.getCode());
        //设置时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(employeeId);
//        employee.setUpdateUser(employeeId);
        baseMapper.insert(employee);
    }

    @Override
    public Page<Employee> findPageList(Integer page, Integer pageSize, String name) {
        Page<Employee> page1 = new Page<>(page, pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("update_time");
        Page<Employee> employeePage = baseMapper.selectPage(page1, wrapper);
        return employeePage;
    }

    @Override
    public void updateStatus(EmployeeDTO employeeDTO, Long employeeId) {
        Employee employee = new Employee();
        employee.setId(employeeDTO.getId());
        employee.setStatus(employeeDTO.getStatus());
        employee.setName(employeeDTO.getName());
        employee.setUsername(employeeDTO.getUsername());
        employee.setIdNumber(employeeDTO.getIdNumber());
        employee.setPhone(employeeDTO.getPhone());

//        employee.setUpdateUser(employeeId);
//        employee.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(employee);
    }


}
